﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AxWMPLib;

//https://www.toutiao.com/i6357276590774157826/?group_id=6357976254818631938
namespace music_player
{
    public partial class MainForm : Form
    {
        private List<string> song_list_ = new List<string>();
        private List<string> pic_list_ = new List<string>();
        private bool is_pause_ = false;
        public MainForm()
        {
            InitializeComponent();
            // Init background picture
            pic_list_.Add("D:\\git\\c_shape\\winform_demo\\music_player\\music_player\\resources\\1.jpg");
            pboxDisplay.ImageLocation = pic_list_[0];
            pboxDisplay.Show();

        }
        private void btnLastSong_Click(object sender, EventArgs e)
        {
            int index = lbxSongList.SelectedIndex;
            if (index <= 0)
            {// If havn't exist selected item, play the last one.
                int next_index_ = song_list_.Count - 1;
                lbxSongList.SelectedIndex = next_index_;
                playMusic_(song_list_[next_index_]);
            }
            else
            {// If exist selected item in play list, then play it.
                int next_index_ = index-1;
                lbxSongList.SelectedIndex = next_index_;
                playMusic_(song_list_[next_index_]);
            }
        }

        private void btnNextSong_Click(object sender, EventArgs e)
        {
            int index = lbxSongList.SelectedIndex;
            if (index<0 || index>=song_list_.Count-1 )
            {// If havn't exist selected item OR illegal index OR selected is last one, play the first one.
                int next_index_ = 0;
                lbxSongList.SelectedIndex = next_index_;
                playMusic_(song_list_[next_index_]);
            }
            else
            {// OR, then play next one.
                int next_index_ = index + 1;
                lbxSongList.SelectedIndex = next_index_;
                playMusic_(song_list_[next_index_]);
            }
        }
        private string findSongName(string path) {
            return path.Split('\\').Last();
        }
        private void btnAddSong_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Title = "Choose new file";
            dialog.ShowHelp = true;
            dialog.Filter = "mp3 file|*.mp3";
            dialog.FilterIndex = 1;
            dialog.RestoreDirectory = false;
            dialog.InitialDirectory = ".\\";
            dialog.Multiselect = true;

            if (dialog.ShowDialog() == DialogResult.OK) {
                // Show those songs to SongList
                // ...
                string[] file_list = dialog.FileNames;
                foreach(string file in file_list){
                    if (! song_list_.Contains(file)){
                        // If file hasn't contained at song_list_, add it
                        song_list_.Add(file);
                    }
                }
                // Update song list.
                if (song_list_.Count > 0)
                {
                    updatePlayList_();
                    btnNextSong.Enabled = true;
                    btnLastSong.Enabled = true;
                    axMusicPlayer.Ctlenabled = true;
                }
            }
        }

        private void axMediaPlayer_click(object sender, EventArgs e)
        {
            int index = lbxSongList.SelectedIndex;
            if (index >= 0)
            {// If exist selected item in play list, then play it.
                playMusic_(song_list_[index]);
            }
            else
            {// OR, play the first one.
                lbxSongList.SelectedIndex = 0;
                playMusic_(song_list_[0]);
            }
        }

        private void axMediaPlayer_Enter(object sender, EventArgs e)
        {
        }
        private void lbxSongList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void playMusic_(string path) {
            if (System.IO.File.Exists(path)) {// If file exist
                axMusicPlayer.URL = path;
                axMusicPlayer.Ctlcontrols.play();
            }
            else {
                MessageBox.Show("File isn't exist.","Warning");
                song_list_.Remove(path);
                updatePlayList_();
            }
        }
        private void updatePlayList_() {
            lbxSongList.Items.Clear();
            foreach (string file in song_list_)
            {
                lbxSongList.Items.Add(findSongName(file));
            }
        }
        private void updatePicBox_(string path) {
            //TODO
        }
        private void timerPic_Tick(object sender, EventArgs e)
        {
            string pic_path = "";
            updatePicBox_(pic_path);
        }
        private void lbxSongList_DoubleClick(object sender, EventArgs e)
        {
            int index = lbxSongList.SelectedIndex;
            if (index >= 0)
            {
                playMusic_(song_list_[index]);
            }
        }

        private void btnPause_Click(object sender, EventArgs e)
        {
            //if (!is_pause_)
            //{
            //    is_pause_ = true;
            //    axMusicPlayer.Ctlcontrols.pause();
            //}
            //else {
            //    is_pause_ = false;
            //    axMusicPlayer.Ctlcontrols.play();
            //}
        }
        private void axMusicPlayer_EndOfStream(object sender, _WMPOCXEvents_EndOfStreamEvent e)
        {
            MessageBox.Show("xxxx");
        }

        private void lbxSongList_Click(object sender, EventArgs e)
        {
            int index = lbxSongList.SelectedIndex;
            if (index >= 0) {
                axMusicPlayer.URL = song_list_[index];
            }
        }
    }
}
